package com.gla.giliair.web.admin;

import com.gla.giliair.entity.Admin;
import com.gla.giliair.util.MD5Util;
import com.gla.giliair.util.MapGenerator;
import com.gla.giliair.web.BaseController;
import com.gla.giliair.web.vo.AdminVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/25 20:39
 */
@Api(tags = "管理员管理模块")
@RestController
@RequestMapping("/api")
public class AdminController extends BaseController {

    @ApiOperation(value = "登录", notes = "登录", produces = "application/json")
    @PostMapping("/adminLoginIn")
    @ResponseBody
    public Map<String, Object> loginIn(@RequestParam("username") String username,
                                       @RequestParam("password") String password,
                                       HttpSession httpSession) {
        if (!StringUtils.hasText(username)) {
            return MapGenerator.getRes(500, "请输入管理员用户名！");
        }
        if (!StringUtils.hasText(password)) {
            return MapGenerator.getRes(501, "请输入密码！");
        }
        return adminService.checkLogin(username, MD5Util.md5Encode(password), httpSession);
    }

    @ApiOperation(value = "登出", notes = "登出", produces = "application/json")
    @PostMapping("/admin/loginOut")
    @ResponseBody
    public Map<String, Object> loginOut(HttpSession httpSession) {
        httpSession.removeAttribute("admin");
        return MapGenerator.getRes(200, "管理员登出成功！");
    }

    @ApiOperation(value = "获取已登录的管理员信息", notes = "从session中拿取管理员VO", produces = "application/json")
    @GetMapping("/admin/getInfo")
    @ResponseBody
    public Map<String, Object> getInfo(HttpSession httpSession) {
        AdminVO userTemp = (AdminVO) httpSession.getAttribute("admin");
        System.out.println(httpSession);
        if (userTemp != null) {
            return MapGenerator.getResWithData(200, "获取管理员信息成功！", userTemp);
        } else {
            return MapGenerator.getRes(500, "未登录！");
        }
    }

    @ApiOperation(value = "获取全部管理员列表", notes = "获取全部管理员列表，有多种排序方式", produces = "application/json")
    @GetMapping("/adminX/findAllAdmin")
    @ResponseBody
    public Map<String, Object> findAllAdmin(//排序关键词：adminId、username、password、nickname、role
                                            // createTime、updateTime、isLocked
                                            //不填关键词默认按id逆向排序
                                            @RequestParam(value = "sort", defaultValue = "adminId") String sort,
                                            //正反序只对关键词生效
                                            @RequestParam(value = "isASC", defaultValue = "false") boolean isASC,
                                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "pageSize", defaultValue = "16") Integer pageSize) {
        return adminService.findAllAdmins(sort, isASC, page, pageSize);
    }

    @ApiOperation(value = "搜索管理员列表", notes = "搜索管理员列表，有多种排序方式", produces = "application/json")
    @PostMapping("/adminX/searchAdmin")
    @ResponseBody
    public Map<String, Object> searchAdmin(//排序关键词：adminId、username、password、nickname、role
                                           // createTime、updateTime、isLocked
                                           //不填关键词默认按id逆向排序
                                           @RequestParam(value = "sort", defaultValue = "adminId") String sort,
                                           //正反序只对关键词生效
                                           @RequestParam(value = "isASC", defaultValue = "false") boolean isASC,
                                           @RequestParam(value = "page", defaultValue = "1") Integer page,
                                           @RequestParam(value = "pageSize", defaultValue = "16") Integer pageSize,
                                           //模糊搜索参数：用户名
                                           @RequestBody AdminVO searchParams) {
        return adminService.searchAdmins(sort, isASC, page, pageSize, searchParams);
    }

    @ApiOperation(value = "添加管理员信息", notes = "添加管理员信息", produces = "application/json")
    @PostMapping("/adminX/addAdmin")
    @ResponseBody
    public Map<String, Object> addAdmin(@RequestParam("username") String username,
                                        @RequestParam("password") String password,
                                        @RequestParam(value = "nickname", required = false) String nickname,
                                        //不填权限，默认为0
                                        @RequestParam(value = "role", required = false) Integer role) {
        if (!StringUtils.hasText(username)) {
            return MapGenerator.getRes(500, "请输入登录用户名！");
        }
        if (!StringUtils.hasText(password)) {
            return MapGenerator.getRes(501, "请输入密码！");
        }
        if (!new Integer(0).equals(role) && !new Integer(1).equals(role) && role != null) {
            return MapGenerator.getRes(502, "管理员权限不存在！");
        }
        if (!StringUtils.hasText(nickname)) {
            nickname = username;
        }

        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(MD5Util.md5Encode(password));
        admin.setNickname(nickname);
        admin.setRole(role);

        return adminService.addAdmin(admin);
    }

    @ApiOperation(value = "修改管理员信息", notes = "修改管理员信息", produces = "application/json")
    @PostMapping("/adminX/modifyAdmin")
    @ResponseBody
    public Map<String, Object> modifyAdmin(@RequestParam("adminId") Long adminId,
                                           @RequestParam(value = "username", required = false) String username,
                                           @RequestParam(value = "password", required = false) String password,
                                           @RequestParam(value = "nickname", required = false) String nickname,
                                           @RequestParam(value = "role", required = false) Integer role) {
        if (adminId == null) {
            return MapGenerator.getRes(500, "请输入管理员id！");
        }
        if (!new Integer(0).equals(role) && !new Integer(1).equals(role) && role != null) {
            return MapGenerator.getRes(502, "管理员权限不存在！");
        }
        Admin admin = new Admin();
        admin.setAdminId(adminId);
        if (StringUtils.hasText(username)) {
            admin.setUsername(username);
        }
        if (StringUtils.hasText(password)) {
            admin.setPassword(MD5Util.md5Encode(password));
        }
        if (StringUtils.hasText(nickname)) {
            admin.setNickname(nickname);
        }
        admin.setRole(role);

        return adminService.modifyAdmin(admin);
    }

    @ApiOperation(value = "冻结/解冻管理员信息", notes = "冻结/解冻管理员信息", produces = "application/json")
    @PostMapping("/adminX/lockAdmin")
    @ResponseBody
    public Map<String, Object> lockAdmin(@RequestParam("adminId") Long adminId,
                                         //不填是否冻结，默认冻结
                                         @RequestParam(value = "isLocked", defaultValue = "true") boolean isLocked) {
        if (adminId == null) {
            return MapGenerator.getRes(500, "请输入管理员id！");
        }
        Admin admin = new Admin();
        admin.setAdminId(adminId);
        if (isLocked) {
            admin.setIsLocked(1);
        } else {
            admin.setIsLocked(0);
        }
        Map<String, Object> res = adminService.modifyAdmin(admin);
        if ((Integer) res.get("status") == 200) {
            if (isLocked) {
                return MapGenerator.getRes(200, "冻结管理员成功!");
            } else {
                return MapGenerator.getRes(201, "解冻管理员成功!");
            }
        }
        return res;
    }

    @ApiOperation(value = "删除管理员信息", notes = "删除管理员信息，真删除", produces = "application/json")
    @PostMapping("/adminX/deleteAdmin")
    @ResponseBody
    public Map<String, Object> deleteAdmin(@RequestParam("adminId") Long adminId) {
        if (adminId == null) {
            return MapGenerator.getRes(500, "请输入管理员id！");
        }
        return adminService.deleteAdmin(adminId);
    }

}
